package com.pandi.springbootinfluxdb.v01;

import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author pandi
 * @create 2021-04-19 15:07
 */
//@RestController
public class TestController {

//   @Autowired
    private InfluxDBConnection connection;


    /**
     * 查询全部
     * @return
     */
   @GetMapping("queryAll")
    public String queryAll(){
       QueryResult query = connection.query("select * from autogen.test");
       query.getResults().forEach( result -> {

           result.getSeries().forEach(series -> {
               List<List<Object>> values = series.getValues();
               values.forEach(value ->{
                   System.out.println(value);
               });

           });
       });
       return "ok";
   }

    /**
     * 插入单条数据
     */
    @GetMapping("insert")
    public String insert(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("host","127.0.0.1");
        hashMap.put("monitor_name","pp");
        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("count",5);
        connection.insert("test",hashMap,hashMap2, System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        return "ok";
    }

    /**
     * 批量插入数据
     */
    @GetMapping("insertAll")
    public String insertAll(){
        HashMap<String, String> tags = new HashMap<>();
        tags.put("host","127.0.0.1");
        tags.put("monitor_name","7777");
        HashMap<String, Object> filed = new HashMap<>();
        filed.put("count",8);
        HashMap<String, Object> filed2 = new HashMap<>();
        filed2.put("count",8);
        Point test1 = connection.pointBuilder("test", System.currentTimeMillis(), tags, filed);
        Point test = connection.pointBuilder("test", System.currentTimeMillis(), tags, filed2);
        BatchPoints build = BatchPoints.database("pandi").tag("host", "127.0.0.1").tag("monitor_name","666")
                .retentionPolicy("autogen").consistency(InfluxDB.ConsistencyLevel.ALL)
                .build();
        BatchPoints build1 = BatchPoints.database("pandi").tag("host", "127.0.0.1").tag("monitor_name","666")
        .retentionPolicy("autogen").consistency(InfluxDB.ConsistencyLevel.ALL).build();
        build.point(test);
        build1.point(test1);
        connection.batchInsert(build);
        connection.batchInsert(build1);
        return "ok";
    }





























}
